//
//  Base_TitleBar.m
//  TeamInformatization
//
//  Created by 李光磊 on 2019/4/3.
//  Copyright © 2019 SPIC. All rights reserved.
//

#import "Base_TitleBar.h"

@implementation Base_TitleBar

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        backgroundV = [[UIView alloc] initWithFrame:self.bounds];
        backgroundV.backgroundColor = kUIColorHex(0x576B95);
        [self addSubview:backgroundV];
        
//如果状态栏需要变色
//        UIView * statusBarView = [[UIView alloc] init];
//        statusBarView.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, kStatusBarHeight);
//        statusBarView.backgroundColor = kUIBlueColor;
//        [backgroundV addSubview:statusBarView];
        
        titleLabel = [[UILabel alloc] init];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = kFont(PFSCRegular, 18);
        titleLabel.textColor = kUIColorHex(0xFFFFFF);
        [self addSubview:titleLabel];

        
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.backgroundColor = [UIColor clearColor];
        [_leftButton addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self resetButton:_leftButton];
        [self addSubview:_leftButton];
        
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.backgroundColor = [UIColor clearColor];
        [_rightButton addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self resetButton:_rightButton];
        [self addSubview:_rightButton];
        
        _rightSecondButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightSecondButton.backgroundColor = [UIColor clearColor];
        [_rightSecondButton addTarget:self action:@selector(rightSecondBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self resetButton:_rightSecondButton];
        [self addSubview:_rightSecondButton];
        
        self.leftButtonStyle = Base_NavigationBar_None;
        self.rightButtonStyle = Base_NavigationBar_None;
        self.rightSecondButtonStyle = Base_NavigationBar_None;
        
    }
    return self;
}

- (void)resetButton:(UIButton *)button {
    
    [button setImage:nil forState:UIControlStateNormal];
    [button setImage:nil forState:UIControlStateHighlighted];
    [button setImage:nil forState:UIControlStateDisabled];
    [button setImage:nil forState:UIControlStateSelected];
    
    [button setBackgroundImage:nil forState:UIControlStateNormal];
    [button setBackgroundImage:nil forState:UIControlStateHighlighted];
    [button setBackgroundImage:nil forState:UIControlStateDisabled];
    [button setBackgroundImage:nil forState:UIControlStateSelected];
    
    [button setTitle:nil forState:UIControlStateNormal];
    [button setTitle:nil forState:UIControlStateHighlighted];
    [button setTitle:nil forState:UIControlStateDisabled];
    [button setTitle:nil forState:UIControlStateSelected];
    
    [button setTitleColor:kUIColorHex(0xFFFFFF) forState:UIControlStateNormal];
//    [button setTitleColor:kUIRGB999Color forState:UIControlStateHighlighted];
//    [button setTitleColor:kUINavitionBarDisEnableColor forState:UIControlStateDisabled];
//    [button setTitleColor:kUIRGB999Color forState:UIControlStateSelected];
    button.userInteractionEnabled=YES;//交互开
    button.imageEdgeInsets = UIEdgeInsetsZero;
    button.titleEdgeInsets = UIEdgeInsetsZero;
    button.titleLabel.font = kFont(PFSCRegular, 16);
    button.layer.cornerRadius = 0.0f;
    button.layer.borderWidth = 0.0f;
    button.layer.borderColor = [UIColor clearColor].CGColor;
}

#pragma mark - setter
- (void)setTitle:(NSString *)title{
    
    _title = title;
    titleLabel.text = title;
    [self resetTitleAndImage];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor{
    backgroundV.backgroundColor = backgroundColor;
}

- (void)setTitleColor:(UIColor *)titleColor{
    
    titleLabel.textColor = titleColor;
}

- (void)setBackGroundAlpha:(CGFloat)backGroundAlpha{
    _backGroundAlpha = backGroundAlpha;
    backgroundV.alpha = backGroundAlpha;
    NSArray *subviews = [self subviews];
    [subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        NSString *classStr = NSStringFromClass([view class]);
        if ([classStr isEqualToString:@"_UIBarBackground"]) {
            view.alpha = backGroundAlpha;
            *stop = YES;
        }
    }];
}

- (void)setTitleImage:(UIImage *)titleImage{
    
    _titleImage = titleImage;
    titleImageView.image = titleImage;
    [self resetTitleAndImage];
}

- (void)resetTitleAndImage{
    
    CGFloat titleTop = StatusBarHeight+10.0f;
    if (self.title.length > 0 && (self.titleImage || self.titleImageURL.length > 0)) {
        [titleLabel sizeToFit];
        float width = MIN(CGRectGetWidth(titleLabel.bounds), CGRectGetWidth(self.bounds) - 170);
        titleImageView.frame = CGRectMake((CGRectGetWidth(self.bounds) - (width + 29)) / 2, titleTop, 24, 24);
        titleLabel.frame = CGRectMake(CGRectGetMaxX(titleImageView.frame) + 5, titleTop, width, 24);
        titleLabel.hidden = NO;
        titleImageView.hidden = NO;
        titleImageView.layer.cornerRadius = CGRectGetHeight(titleImageView.bounds) / 2;
        titleImageView.layer.masksToBounds = YES;
    }
    else if (self.title.length >0) {
        
        [titleLabel sizeToFit];
        CGFloat width = MIN(CGRectGetWidth(titleLabel.bounds), CGRectGetWidth(self.bounds) - 120);
        titleLabel.frame = CGRectMake((CGRectGetWidth(self.bounds) - width) / 2, titleTop, width, 24);
        titleLabel.hidden = NO;
        titleImageView.hidden = YES;
    }
    else if (self.titleImage || self.titleImageURL.length > 0){
        
        [titleImageView sizeToFit];
        titleImageView.backgroundColor = [UIColor clearColor];
        titleImageView.frame = CGRectMake((CGRectGetWidth(self.bounds) - CGRectGetWidth(titleImageView.bounds)) / 2, titleTop, CGRectGetWidth(titleImageView.bounds), CGRectGetHeight(titleImageView.bounds));
        titleLabel.hidden = YES;
        titleImageView.hidden = NO;
        titleImageView.layer.cornerRadius = 0;
        titleImageView.layer.masksToBounds = YES;
    }
}

- (void)setLeftButtonStyle:(Base_NavigationBarStyle)leftButtonStyle{
    
    _leftButtonStyle = leftButtonStyle;
    [self changeButtonStyle:leftButtonStyle withButton:_leftButton];
    
}

- (void)setRightButtonStyle:(Base_NavigationBarStyle)rightButtonStyle{
    _rightButtonStyle = rightButtonStyle;
    [self changeButtonStyle:rightButtonStyle withButton:_rightButton];
}

-(void)setRightSecondButtonStyle:(Base_NavigationBarStyle)rightSecondButtonStyle{
    _rightSecondButtonStyle = rightSecondButtonStyle;
    [self changeButtonStyle:rightSecondButtonStyle withButton:_rightSecondButton];
}

- (void)changeButtonStyle:(Base_NavigationBarStyle)style withButton:(UIButton *)button{
    
    button.hidden = NO;
    [self resetButton:button];
    switch (style) {
        case Base_NavigationBar_Back:
        {
            [button setImage:[UIImage imageNamed:@"Group"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"Group"] forState:UIControlStateHighlighted];
            [button setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0.0f, 5)];
        }
            break;
        case Base_NavigationBar_Back_Home:
        {
            [button setImage:[UIImage imageNamed:@"home"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"home"] forState:UIControlStateHighlighted];
        }
            break;
            
        default:
            break;
    }
    [self resizeButton:button];
}

- (void)resizeButton:(UIButton *)button {

    [button sizeToFit];
    BOOL onlyImage = (button.titleLabel.text.length == 0 || button.layer.borderWidth > 0 );
    
    CGFloat buttonWidth = (button.titleLabel.text.length == 0)? MAX(CGRectGetWidth(button.bounds), 40.0f) : CGRectGetWidth(button.bounds) + 25.0f;
    
    CGFloat buttonHeight = 30.0f;//(button.titleLabel.text.length == 0) ?CGRectGetHeight(button.bounds) : 24.0f;
    
    float height = self.bounds.size.height;
    CGFloat buttonTop = (height-StatusBarHeight-buttonHeight) / 2 + StatusBarHeight;
    
    if ([button isEqual:_leftButton]) {
        button.frame = CGRectMake((onlyImage ? 8 : 0), buttonTop, buttonWidth, buttonHeight);
        _logoImageV.frame = CGRectMake(button.right, 11+StatusBarHeight, 74, 22);
        
    } else if ([button isEqual:_rightButton]) {
        button.frame = CGRectMake(CGRectGetWidth(self.bounds) - buttonWidth - (onlyImage ? 8 : 0), buttonTop, buttonWidth, buttonHeight);
        _rightSecondButton.frame = CGRectMake(CGRectGetWidth(self.bounds) - buttonWidth - 8  - CGRectGetWidth(_rightSecondButton.bounds), CGRectGetMinY(_rightSecondButton.frame), CGRectGetWidth(_rightSecondButton.bounds), CGRectGetHeight(_rightSecondButton.bounds));
    } else if ([button isEqual:_rightSecondButton]){
        button.frame = CGRectMake(CGRectGetMinX(_rightButton.frame) - buttonWidth, buttonTop, buttonWidth, buttonHeight);
    }
}
#pragma mark - Private

- (void)leftBtnClick:(UIButton *)sender{
    
    if (self.titleBardelegate && [self.titleBardelegate respondsToSelector:@selector(baseNavigationBarLeftButtonClick:)]) {
        [self.titleBardelegate baseNavigationBarLeftButtonClick:sender];
    }
}

- (void)rightBtnClick:(UIButton *)sender{
    
    if (self.titleBardelegate && [self.titleBardelegate respondsToSelector:@selector(baseNavigationBarRightButtonClick:)]) {
        [self.titleBardelegate baseNavigationBarRightButtonClick:sender];
    }
}

- (void)rightSecondBtnClick:(UIButton *)sender{
    
    if (self.titleBardelegate && [self.titleBardelegate respondsToSelector:@selector(baseNavigationBarRightSecondButtonClick:)]) {
        [self.titleBardelegate baseNavigationBarRightSecondButtonClick:sender];
    }
}

@end
